package com.dpad.crmclientapp.android.modules.newapp.presenter


import com.dpad.crmclientapp.android.modules.newapp.ui.fragment.THomeFragment
import com.dpad.telematicsclientapp.mvp.kit.Kits
import com.dpad.telematicsclientapp.mvp.mvp.XPresent
import com.dpad.telematicsclientapp.netlibrary.MainApplicaton
import com.dpad.telematicsclientapp.netlibrary.http.EncryptUtils.SecretResult
import com.dpad.telematicsclientapp.netlibrary.http.NetInstance
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.HomeBean
import com.dpad.telematicsclientapp.netlibrary.newapp.entity.YckzZipEntity
import com.dpad.telematicsclientapp.util.utils.ParameterUtils
import com.socks.library.KLog
import io.reactivex.Observer
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.Disposable
import io.reactivex.schedulers.Schedulers
import java.util.*

/**
 * ================================================
 * 作    者：booob
 * 版    本：1.0
 * 创建日期：2018-05-18-0018 17:30
 * 描    述：
 * 修订历史：
 * ================================================
 */
class HomeFragmentPresenter : XPresent<THomeFragment>() {

    fun getHomeData(map: SortedMap<String, Any>) {
        KLog.e("首页请求开始", System.currentTimeMillis().toString())
        NetInstance.getEventsService().getHomeData(ParameterUtils.getHeaser(map), ParameterUtils.getJsonBody(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<HomeBean> {
                    override fun onComplete() {
                        v.loadingComplete()
                        KLog.e("首页请求完成", System.currentTimeMillis().toString())
                    }

                    override fun onSubscribe(d: Disposable?) {
                        addDisposable(d)
                    }

                    override fun onNext(value: HomeBean?) {
                        value!!.let {
                            if (value.code == "00000") {
                                if (!Kits.Empty.check(MainApplicaton.urlMap["indexvehicleinfo"])) {
                                    value.result.carPic = MainApplicaton.urlMap["indexvehicleinfo"]
                                }
                                v.showData(value)
//                                getCarPicUrl(value)
                            }
                        }
                    }

                    override fun onError(e: Throwable?) {
                        v.loadingComplete()
                    }

                })
    }

    fun getHomeDataWithNoToken(map: SortedMap<String, Any>) {
        NetInstance.getEventsService().getHomeDataWithNoToken(ParameterUtils.getHeaser(map), ParameterUtils.getJsonBody(map))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<HomeBean> {
                    override fun onComplete() {
                        v.loadingComplete()
                    }

                    override fun onSubscribe(d: Disposable?) {
                        addDisposable(d)
                    }

                    override fun onNext(value: HomeBean?) {
                        value!!.let {
                            if (value.code == "00000") {
                                v.showData(value)
                            }
                        }
                    }

                    override fun onError(e: Throwable?) {
                        v.loadingComplete()
                    }

                })
    }


    /**
     * 检查是否设置了pin码
     *
     * @param sortedMap
     */
    fun checkHasPin(sortedMap: SortedMap<String, Any>) {
        v.loading("正在加载...")
        NetInstance.getRemoteControlService()
                .checkHasPin(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getSecretJsonBody(sortedMap))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<SecretResult> {
                    override fun onSubscribe(d: Disposable) {
                        addDisposable(d)
                    }

                    override fun onNext(value: SecretResult) {
                        if (value.code == "00000") {
                            v.showCheckHasSettingPin(value)
                        }
                    }

                    override fun onError(e: Throwable) {
                        v.loadingComplete()
                        v.dealHttpError(e)
                    }

                    override fun onComplete() {
                        v.loadingComplete()
                    }
                })
    }

    /**
     * 检查是否设置绑定过设备
     *
     * @param sortedMap
     */
    fun confirmDeviceBinded(sortedMap: SortedMap<String, Any>) {
        v.loading("正在加载...")
        NetInstance.getRemoteControlService()
                .confirmDeviceBinded(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getSecretJsonBody(sortedMap))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<SecretResult> {
                    override fun onSubscribe(d: Disposable) {
                        addDisposable(d)
                    }

                    override fun onNext(value: SecretResult) {
                        if (value.code == "00000") {
                            v.dealBindDevice(value)
                        }
                    }

                    override fun onError(e: Throwable) {
                        v.loadingComplete()
                        v.dealHttpError(e)
                    }

                    override fun onComplete() {
                        v.loadingComplete()
                    }
                })
    }

    /**
     * 校验pin码是否正确
     *
     * @param sortedMap
     */
    fun checkPin(sortedMap: SortedMap<String, Any>) {
        v.loading("正在检验...")
        NetInstance.getRemoteControlService().checkPin(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getSecretJsonBody(sortedMap)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : Observer<SecretResult> {
            override fun onSubscribe(d: Disposable) {
                addDisposable(d)
            }

            override fun onNext(value: SecretResult) {
                if (value.code == "00000") {
                    v.pinCheckSuccess()
                } else {
                    v.pinCheckFail()
                }
            }

            override fun onError(e: Throwable) {
                v.loadingComplete()
                v.dealHttpError(e)
                v.pinCheckFail()
            }

            override fun onComplete() {
                v.loadingComplete()
            }
        })
    }

    //下载zip包
    fun getCarPicUrl(value: HomeBean) {
//        populevehicle 我的爱车车图
//        uservehicle 个人中心车图
//        ecovehicle 环保驾驶车图
//        indexvehicleinfo 首页车况报告车图
//        vehicleinfoblue 车况报告车图蓝底
//        vehicleinfored 车况报告车图红底
//        vehicleinfoyellow 车况报告车图黄底

        v.loading("正在加载...")
        val sortedMap: SortedMap<String?, Any?> = TreeMap<String?, Any?>()
        sortedMap["saleSubmodelId"] = MainApplicaton.getSaleSubmodelId()
        sortedMap["downloadType"] = "1" //0为zip 1为车型图片 2为远控图片
        NetInstance.getEventsService().picDownload(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<YckzZipEntity> {
                    override fun onSubscribe(d: Disposable) {
                        addDisposable(d)
                    }

                    override fun onNext(bean: YckzZipEntity) {
                        if (bean.code == "00000") {
                            MainApplicaton.urlMap.clear()
                            if (!bean.result.isNullOrEmpty()) {
                                for (item in bean.result) {
                                    if (!Kits.Empty.check(item.mark)) {
                                        when (item.mark) {
                                            "populevehicle" -> {
                                                MainApplicaton.urlMap["populevehicle"] = item.downloadPath
                                            }
                                            "uservehicle" -> {
                                                MainApplicaton.urlMap["uservehicle"] = item.downloadPath
                                            }
                                            "ecovehicle" -> {
                                                MainApplicaton.urlMap["ecovehicle"] = item.downloadPath
                                            }
                                            "indexvehicleinfo" -> {
                                                MainApplicaton.urlMap["indexvehicleinfo"] = item.downloadPath
                                                value.result.carPic = item.downloadPath
                                            }
                                            "vehicleinfoblue" -> {
                                                MainApplicaton.urlMap["vehicleinfoblue"] = item.downloadPath
                                            }
                                            "vehicleinfored" -> {
                                                MainApplicaton.urlMap["vehicleinfored"] = item.downloadPath
                                            }
                                            "vehicleinfoyellow" -> {
                                                MainApplicaton.urlMap["vehicleinfoyellow"] = item.downloadPath
                                            }
                                        }
                                    }
                                }
                            }
                            v.showData(value)
                        }
                    }

                    override fun onError(e: Throwable) {
                        v.loadingComplete()
                        v.dealHttpError(e)
                    }

                    override fun onComplete() {
                        v.loadingComplete()
                    }
                })
    }


    //下载zip包
    fun chaxun() {
        v.loading("正在加载...")
        val sortedMap: SortedMap<String, Any> = TreeMap()
        //        sortedMap.put("saleSubmodelId", "1pwta4");
        sortedMap.put("saleSubmodelId", MainApplicaton.getSaleSubmodelId())
        sortedMap.put("downloadType", "0")//0为zip 1为车型图片 2为远控图片

        NetInstance.getEventsService().picDownload(ParameterUtils.getHeaser(sortedMap), ParameterUtils.getJsonBody(sortedMap)).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(object : Observer<YckzZipEntity> {
            override fun onSubscribe(d: Disposable) {
                addDisposable(d)
            }

            override fun onNext(bean: YckzZipEntity) {
                if (bean.code == "00000" && bean.result != null && bean.result.size > 0) {
                    v.showDownload(bean.result)
                } else {
                    v.downZipFail()
                }
            }

            override fun onError(e: Throwable) {
                v.loadingComplete()
                v.dealHttpError(e)
                v.downZipFail()
            }

            override fun onComplete() {
                v.loadingComplete()
            }
        })
    }


}
